define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

    var Controller = {
			index: function () {
				
					// 基于准备好的dom，初始化echarts实例
				var myChart = Echarts.init(document.getElementById('echart'), 'walden');

				// 指定图表的配置项和数据
				var option = {
					title: {
						text: '',
						subtext: ''
					},
					tooltip: {
						trigger: 'axis'
					},
					legend: {
						data: [__('Sales'), __('Orders')]
					},
					toolbox: {
						show: false,
						feature: {
							magicType: {show: true, type: ['stack', 'tiled']},
							saveAsImage: {show: true}
						}
					},
					xAxis: {
						type: 'category',
						boundaryGap: false,
						data: Orderdata.column
					},
					yAxis: {

					},
					grid: [{
							left: 'left',
							top: 'top',
							right: '10',
							bottom: 30
						}],
					series: [{
							name: __('Sales'),
							type: 'line',
							smooth: true,
							areaStyle: {
								normal: {
								}
							},
							lineStyle: {
								normal: {
									width: 1.5
								}
							},
							data: Orderdata.paydata
						},
						{
							name: __('Orders'),
							type: 'line',
							smooth: true,
							areaStyle: {
								normal: {
								}
							},
							lineStyle: {
								normal: {
									width: 1.5
								}
							},
							data: Orderdata.createdata
						}]
				};
			
				//使用刚指定的配置项和数据显示图表。
				myChart.setOption(option);
		
			var myChart7 = Echarts.init(document.getElementById('data7')); //游戏占比数据统计 基于准备好的dom，初始化echarts实例	
			var uploadedDataURL7 = "/admin/recharge/Api/proportion"; 
			setInterval(function () {
				$.getJSON(uploadedDataURL7, function(CTmpData) {
					var builderJson = CTmpData.builderJson;
					var downloadJson = CTmpData.downloadJson;
					var themeJson = CTmpData.themeJson;
					var onlineUsers = CTmpData.onlineUsers; //在线人数;
					var roomCount = builderJson.roomCount; //在线开房数;
					
					/*  $.ajax({
						url: Config.GameDB.api_url + 'onlineUsers',
						type: 'GET',
						dataType: 'json',
						success: function (ret) {
								//debugger;
							sevendau=ret;
						}
					}); 
					//获取开房数量
					  $.ajax({
						url: Config.GameDB.api_url + 'gamingUser',
						type: 'GET',
						dataType: 'json',
						success: function (ret) {
								//debugger;
							for (x in ret)
							{
								roomCount = roomCount+ret[x].roomCount;
							}
						}
					});  */
					
					Orderdata.column.push((new Date()).toLocaleTimeString().replace(/^\D*/, '')); //时间

					Orderdata.paydata.push(onlineUsers); //在线人数
					Orderdata.createdata.push(roomCount);//开房数
					
					//按自己需求可以取消这个限制
					if (Orderdata.column.length >= 20) {
						//移除最开始的一条数据
						Orderdata.column.shift();
						Orderdata.paydata.shift();
						Orderdata.createdata.shift();
					}
					myChart.setOption({
						xAxis: {
							data: Orderdata.column
						},
						
						series: [{
								name: __('Sales'),
								data: Orderdata.paydata
							},
							{
								name: __('Orders'),
								data: Orderdata.createdata
							}]
					});
					
					var option7 = {
							tooltip: {
								
							},
							title: [{
								text: '游戏中的人数',
								subtext: '总计 ' + builderJson.userCount,
								x: '15%',
								textAlign: 'center'
							},{
								text: '游戏中的房间数',
								subtext: '总计 ' + builderJson.roomCount,
								x: '30%',
								textAlign: 'center'
							},{
								text: '在线人数',
								subtext: '总计 ' + onlineUsers,
								x: '45%',
								textAlign: 'center'
							}, {
								text: '游戏开房占比',
								subtext: '总计 ' + Object.keys(downloadJson).reduce(function (all, key) {
									return all + downloadJson[key];
								}, 0),
								x: '75%',
								textAlign: 'center'
							}, {
								text: '游戏人数占比',
								subtext: '总计 ' + Object.keys(themeJson).reduce(function (all, key) {
									return all + themeJson[key];
								}, 0),
								x: '75%',
								y: '50%',
								textAlign: 'center'
							}],
							grid: [{
								top: 50,
								width: '50%',
								bottom: '45%',
								left: 10,
								containLabel: true
							}, {
								top: '55%',
								width: '50%',
								bottom: 0,
								left: 10,
								containLabel: true
							}],
							xAxis: [{
								type: 'value',
								max: builderJson.userCount,
								splitLine: {
									show: true
								}
							}, {
								type: 'value',
								max: builderJson.roomCount,
								gridIndex: 1,
								splitLine: {
									show: true
								}
							}],
							yAxis: [{
								type: 'category',
								data: Object.keys(builderJson.charts),
								axisLabel: {
									interval: 0,
									rotate: 30
								},
								splitLine: {
									show: true
								}
							}, {
								gridIndex: 1,
								type: 'category',
								data: Object.keys(builderJson.components),
								axisLabel: {
									interval: 0,
									rotate: 30
								},
								splitLine: {
									show: true
								}
							}],
							series: [{
								type: 'bar',
								stack: 'chart',
								z: 3,
								label: {
									normal: {
										position: 'right',
										show: true
									}
								},
								data: Object.keys(builderJson.charts).map(function (key) {
									return builderJson.charts[key];
								})
							}, {
								type: 'bar',
								stack: 'chart',
								silent: true,
								itemStyle: {
									normal: {
										color: '#eee'
									}  
								},
								data: Object.keys(builderJson.charts).map(function (key) {
									return builderJson.roomCount - builderJson.charts[key];
								})
							}, {
								type: 'bar',
								stack: 'component',
								xAxisIndex: 1,
								yAxisIndex: 1,
								z: 3,
								label: {
									normal: {
										position: 'right',
										show: true
									}
								},
								data: Object.keys(builderJson.components).map(function (key) {
									return builderJson.components[key];
								})
							}, {
								type: 'bar',
								stack: 'component',
								silent: true,
								xAxisIndex: 1,
								yAxisIndex: 1,
								itemStyle: {
									normal: {
										color: '#eee'
									}  
								},
								data: Object.keys(builderJson.components).map(function (key) {
									return builderJson.roomCount - builderJson.components[key];
								})
							}, {
								type: 'pie',
								radius: [0, '30%'],
								center: ['75%', '25%'],
								data: Object.keys(downloadJson).map(function (key) {
									return {
										name: key.replace('.js', ''),
										value: downloadJson[key]
									}
								}),  
								labelLine:{normal:{show:false}},
								itemStyle: {normal: {label:{ show: true,  formatter: '{b} \n ({d}%)', textStyle:{color:'#B1B9D3'}} },},
							}, {
								type: 'pie',
								radius: [0, '30%'],
								center: ['75%', '75%'],
								data: Object.keys(themeJson).map(function (key) {
									return {
										name: key.replace('.js', ''),
										value: themeJson[key]
									}
								}), 
								labelLine:{normal:{show:false}},
								itemStyle: {normal: {label:{ show: true,  formatter: '{b} \n ({d}%)', textStyle:{color:'#B1B9D3'}} },},
							}]
					};
					
					//使用刚指定的配置项和数据显示图表。
					myChart7.setOption(option7,true);
				});
			}, 2000);
			$(window).resize(function () {
				myChart.resize();	
				myChart7.resize();
			});
				
				

			
	

			
				//读取FastAdmin的更新信息
				$.ajax({
					url: Config.fastadmin.api_url + '/news/index',
					type: 'post',
					dataType: 'jsonp',
					success: function (ret) {
						$("#news-list").html(Template("newstpl", {news: ret.newslist}));
					}
				});
				$.ajax({
					url: Config.fastadmin.api_url + '/forum/discussion',
					type: 'post',
					dataType: 'jsonp',
					success: function (ret) {
						$("#discussion-list").html(Template("discussiontpl", {news: ret.discussionlist.slice(0,6)}));
					}
				});
				

			
			
    };

    return Controller;
});
